$(function () {
    // 渲染用户信息
    getUserInfo()

    // 退出登录
    //layer是layui唯一一个前面不用加layui的
    $('#exit').on('click', function () {
        layer.confirm('确定退出吗?', { icon: 3, title: '提示' }, function (index) {
            //do something

            localStorage.removeItem('token')
            location.href = '/login.html'

            layer.close(index);
        });
    })

})


function getUserInfo() {
    axios({
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token') || ''
        // }

    }).then(function (res) {
       
        renderUserInfo(res.data.data)
    })
}


function renderUserInfo(user) {
    let username = user.nickname || user.username
    // 渲染用户名
    $('.welcome').html('欢迎&nbsp;' + username)
    $('.welcome').show()

    // 渲染头像
    if (!user.user_pic) {
        $('.avatar-text').show().html(username[0].toUpperCase())
        $('.layui-nav-img').hide()
    } else {
        $('.layui-nav-img').show().attr('src', user.user_pic)
        $('.avatar-text').hide()
    }

}